import publicForm from "@/components/public/up-form.vue"; //弹框form
import publicForm1 from "@/components/public/el-form.vue"; //弹框form

import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    getCreditEvaluationProcessByFid, //详情
    saveCreditEvaluationProcess, //保存
    CheckCreditEvaluationProcessByOwner, //业主审核
    checkCreditEvaluationProcessByGroup, //集团审核
    getContractCompanyInfo, //获取建立下的标段信息
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "projectId",
        "status",
        "statusNum",
        "dataFid",
        "currentRow",
        "projectName",
        "stationType"
    ],
    data() {
        return {
            visible: "",
            title: "监理企业年度信用评价",
            isLock: false, // 控制按钮的disable属性
            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            formData: {
                fId: "", //新增时不传或传空
                userId: "", //当前登陆用户ID
                userName: "", //当前登陆用户名称
                companyId: "", //当前登陆用户所属标段ID
                companyName: "", //当前登陆用户所属标段名称
                fJlid: "", //系统自动提取登录监理单位id。
                fJlname: "", //系统自动提取登录监理单位全称。
                fJlenterprisename: "", //手动填写的监理上级企业名称
                fJlprojectid: "", //工程项目ID
                fJlprojectname: "", //工程项目名称
                fJlcompanyid: "", //监理合同段ID
                fJlcompanyname: "", //监理合同段名称
                fStartdates: "", //项目开工时间  年-月-日
                fEnddates: "", //项目交工时间 年-月-日
                fStartdates1: "",
                fContractamount: "", //合同额
                fEvaluateyear: "", //评价年度
                fJlfile: "", //评价相关资料
                fNote: "", //备注
                fJlfillinguserid: "", //填报人ID
                fJlfillingusername: "", //填报人名称
                fJlfillingdates: "", //填报日期 年-月-日 时:分:秒
                fProprietorfile: "", //业主审核相关资料附件
                fGroupfile: "", //集团审批相关资料附件
                recordfiles: [] //删除文件集合
            },
            fWorkfilelist: [], //工作计划资料
            fCcpeopleData: [], //计划传阅人
            fFinishccpeopleData: [], //完成传阅人
            planReview: formJs.planReview, //计划审核
            planReviewData: {
                userId: "", //当前登陆用户ID
                userName: "", //当前登陆用户名称
                fId: "", //当前表的fid
                fProprietoruserid: "", //业主填报人ID
                fProprietorusername: "", //业主填报人名称
                fProprietorcheckdates: this.common.getDay1(0), //业主审核时间 年-月-日 时:分:秒
                fProprietorevaluateopinion: "", //审核意见概述
                fProprietorispass: "", //业主是否通过审核 通过/驳回
                recordfiles: [], //删除文件集合
                fProprietorfile: ""
            }, //计划审核
            planReviewlist: [],
            btnShow: true,
            formFinishObj: formJs.formFinishObj, // 完成情况信息
            formFinishData: {
                fId: "", //新增时不传或传空
                userId: "", //当前登陆用户ID
                userName: "", //当前登陆用户名称
                companyId: "", //当前登陆用户所属标段ID
                companyName: "", //当前登陆用户所属标段名称
                fGroupuserid: "", //集团填报人ID
                fGroupusername: "", //集团填报人名称
                fGroupcheckdates: this.common.getDay1(0), //集团审批时间
                fGroupevaluateopinion: "", //集团审批意见概述
                fGroupispass: "", //集团是否通过审批 通过/驳回
                recordfiles: [] //删除文件集合
            }, // 完成情况信息
            FinishfileList: [], //工作计划资料
            currentStatus: null,
        };
    },
    components: {
        publicForm,
        publicForm1
        // commonPage
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    console.log(888, newVal);
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        stationType: {
            handler(newVal) {
                if (newVal) {
                    this.formData.fType = newVal;
                }
            },
            immediate: true,
        },
        projectId: {
            handler(newVal) {
                if (newVal) {
                    console.log(787, newVal);
                    // this.getPerson(newVal);
                    // this.title = newVal
                }
            },
            immediate: true,
        },
        status: {
            handler(newVal) {
                if (newVal) {
                    this.btnStatus = newVal;
                    // this.title = newVal
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                    // this.title = newVal
                }
            },
            immediate: true,
        },
        dataFid: {
            handler(newVal) {
                if (newVal) {
                    console.log(newVal);
                    this.currntData = newVal;
                    if (this.statusNum != 1) {
                        this.formData = this.$options.data.call(this).formData;

                        console.log(888888);
                        this.getDetail(newVal);
                    }
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    console.log(8989, newVal);
                    this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        console.log('完成情况', this.statusNum);
        this.btnShow = true
        if (this.statusNum == 1) {
            this.formData = this.$options.data.call(this).formData;
            this.getDetail("")

            this.getContractCompanyInfo({
                companyId: this.userInfo.companyId, //监理id
                modelType: "2"
            })
            // // 新增
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[2].disabled = true;
            this.formObj[7].disabled = true;
            this.formObj[8].disabled = true;
            this.formObj[11].disabled = true;
            this.formObj[9].uploadSelect = true;
            this.planReview.map((item) => {
                item.disabled = true;
            });
            this.planReview[6].uploadSelect = false;
            this.formFinishObj.map((item) => {
                item.disabled = true;
            });
            this.formFinishObj[4].uploadSelect = false;

        } else if (this.statusNum == 2) { //编辑
            //编辑
            this.formData = this.$options.data.call(this).formData;
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[2].disabled = true;
            this.formObj[7].disabled = true;
            this.formObj[8].disabled = true;
            this.formObj[11].disabled = true;
            this.formObj[9].uploadSelect = true;
            this.planReview.map((item) => {
                item.disabled = true;
            });
            this.planReview[6].uploadSelect = false;

            this.formFinishObj.map((item) => {
                item.disabled = true;
            });
            this.formFinishObj[4].uploadSelect = false;

        } else if (this.statusNum == 4) { //审核
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[9].uploadSelect = false;



            // this.currentStatus.fStatuss != '审核中'
            if (this.currentStatus.fStatus == "业主审核中") {
                this.planReviewData = this.$options.data.call(this).planReviewData;
                //计划审核
                this.planReviewData.fCheckid = this.userInfo.userId; //审核人id
                this.planReviewData.fCheckname = this.userInfo.realName; //审核人姓名

                this.planReview.map((item) => {
                    item.disabled = false;
                });
                this.planReview[2].disabled = true;
                this.planReview[3].disabled = true;
                this.planReview[6].uploadSelect = true;

                // 完成情况

                this.formFinishObj.map((item) => {
                    item.disabled = true;
                });
                this.formFinishObj[4].uploadSelect = false;
            } else if (this.currentStatus.fStatus == "集团审批中") {
                this.planReview.map((item) => {
                    item.disabled = true;
                });
                this.planReview[6].uploadSelect = false;
                this.formFinishObj.map((item) => {
                    item.disabled = false;
                });
                this.formFinishObj[4].uploadSelect = true;
            }
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[9].uploadSelect = false;
            // 计划审核
            this.planReview.map((item) => {
                item.disabled = true;
            });
            this.planReview[6].uploadSelect = false;

            // 完成情况
            this.formFinishObj.map((item) => {
                item.disabled = true;
            });

            this.formFinishObj[4].uploadSelect = false;


        }

        this.getHeight();
        // this.edit()
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        // fCcpeopleData计划传阅人
        selectChange(data, a, b) {
            if (b == "fJlcompanyname") {
                this.formData.fJlcompanyid = data.value;
                this.formData.fJlcompanyname = data.label;
            }

        },
        edit(status) {
            if (status == 1) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 3) {
                this.formData = this.$options.data.call(this).formData;
            }
        },
        getContractCompanyInfo(data) {
            getContractCompanyInfo(data).then(res => {
                if (res.errcode == 0) {

                    this.formData.fJlprojectname = res.data[0].fCompanyname
                    this.formData.fJlprojectid = res.data[0].fCompanyid
                    this.getContractCompanyInfos({
                        companyId: res.data[0].fCompanyid, //监理id
                        modelType: "1"
                    })
                }
            })
        },
        getContractCompanyInfos(data) {
            getContractCompanyInfo(data).then(res => {
                if (res.errcode == 0) {
                    this.formObj[3].options = []
                    res.data.map(item => {
                        this.formObj[3].options.push({
                            value: item.fCompanyid,
                            label: item.fCompanyname
                        })
                    })


                }
            })
        },
        // 详情
        getDetail(id) {
            getCreditEvaluationProcessByFid({
                fId: id
            }).then((res) => {
                var data = res.data.process;
                if (id != '') { //二次编辑
                    this.dataInfo = data.fId
                    this.formData = data
                    this.formData.fStartdates1 = [data.fStartdates, data.fEnddates]
                    if (data.fProprietorusername == '' || data.fProprietorusername == null) {
                        console.log('ttt');
                        this.planReviewData.userId = this.userInfo.userId; //当前登录人id
                        this.planReviewData.userName = this.userInfo.realName; //当前登录人名称
                        this.planReviewData.fProprietoruserid = this.userInfo.userId; //业主填报人名称id
                        this.planReviewData.fProprietorusername = this.userInfo.realName; //业主填报人名称
                        this.planReviewData.fProprietorcheckdates = this.common.getDay1(0); //业主审核时间
                        this.planReviewData.fProprietorfile = data.fProprietorfile
                    } else {
                        console.log('sss');
                        this.planReviewData = data
                    }
                    if (data.fGroupusername == '' || data.fGroupusername == null) {
                        this.formFinishData.userId = this.userInfo.userId; //当前登陆用户ID
                        this.formFinishData.userName = this.userInfo.realName; //当前登陆用户名称
                        this.formFinishData.companyId = this.userInfo.companyId; //当前登陆用户所属标段ID
                        this.formFinishData.companyName = this.userInfo.companyFullName; //当前登陆用户所属标段名称
                        this.formFinishData.fGroupuserid = this.userInfo.userId; //集团填报人ID
                        this.formFinishData.fGroupusername = this.userInfo.realName; //集团填报人名称
                        this.formFinishData.fGroupcheckdates = this.common.getDay1(0); //集团审批时间
                    } else {
                        this.formFinishData = data


                    }
                } else { //预新增
                    console.log(9999);
                    this.formData.fJlfile = data.fJlfile
                    //新增
                    this.formData.userId = this.userInfo.userId; //当前登录人id
                    this.formData.userName = this.userInfo.realName; //当前登录人名称

                    this.formData.companyName = this.userInfo.companyFullName; //当前登录人标段名称
                    this.formData.companyId = this.userInfo.companyId; //当前登录人标段ID
                    this.formData.fJlname = this.userInfo.companyFullName; //系统自动提取登录监理单位全称。
                    this.formData.fJlid = this.userInfo.companyId; //系统自动提取登录监理单位id。
                    this.formData.fJlprojectname = this.userInfo.projectFullName; //工程项目名称
                    this.formData.fJlprojectid = this.userInfo.projectId; //工程项目ID
                    this.formData.fJlfillinguserid = this.userInfo.userId; //当前登录人id
                    this.formData.fJlfillingusername = this.userInfo.realName; //当前登录人名称
                    this.formData.fJlfillingdates = this.common.getDay1(0); //当前登录人名称
                    this.formData.fEvaluateyear = this.common.getYear(0)
                    this.formData.fGroupfile = data.fGroupfile
                    this.formData.fProprietorfile = data.fProprietorfile
                }

                this.fWorkfilelist = data.jlfiles; //监理
                this.planReviewlist = data.ownerfiles; //业主
                this.FinishfileList = data.groupfiles; //集团

            });
        },
        // 刪除文件
        delteFile(file, num) {
            var arr = [];
            var obj = [];
            if (num == 0) {
                arr = this.fWorkfilelist;
                obj = this.formData;
            } else if (num == 1) {
                arr = this.FinishfileList;
                obj = this.formFinishData;
            }
            for (let index = 0; index < arr.length; index++) {
                if (file.uid == arr[index].uid) {
                    if (arr[index].atta == undefined) {
                        obj.recordfiles.push({
                            fId: arr[index].fId,
                        });
                    } else {
                        obj.recordfiles.push({
                            fId: arr[index].atta.fId,
                        });
                    }
                    arr.splice(index, 1);
                }
            }
        },
        yj_saveForm() {
            if (this.statusNum == 1) { //新增
                this.formData.fStartdates = this.formData.fStartdates1[0]
                this.formData.fEnddates = this.formData.fStartdates1[1]
                if (this.$refs.publicForm.submitForm()) {
                    delete this.formData.fStartdates1;
                    this.saveForm(this.formData, 0);
                } else {
                    console.log("error");
                }
            } else if (this.statusNum == 2) { //编辑
                if (this.$refs.publicForm.submitForm()) {
                    delete this.formData.fCcpeople1;
                    this.saveForm(this.formData, 0);
                } else {
                    console.log("error");
                }
            } else if (this.statusNum == 4) { //审核
                //审核
                if (this.currentStatus.fStatus == "业主审核中") {
                    this.planReviewData.userId = this.userInfo.userId; //当前登录人id
                    this.planReviewData.userName = this.userInfo.realName; //当前登录人名称
                    this.planReviewData.fProprietoruserid = this.userInfo.userId; //业主填报人名称id
                    this.planReviewData.fProprietorusername = this.userInfo.realName; //业主填报人名称
                    this.planReviewData.fId = this.dataInfo
                    if (this.$refs.planReview.submitForm()) {
                        this.yj_examine(this.planReviewData, 0);
                    } else {
                        console.log("error");
                    }
                } else if (this.currentStatus.fStatus == "集团审批中") {
                    // 完成审核
                    this.formFinishData.fId = this.dataInfo

                    if (this.$refs.formFinishForm.submitForm()) {
                        this.yj_examine(this.formFinishData, 1);
                    } else {
                        console.log("error");
                    }
                }
            }
        },

        // 保存
        saveForm(data, status) {
            // 设置按钮不可点击（disable: false）
            this.isLock = true
            if (status == 0) {
                saveCreditEvaluationProcess(data)
                    .then((res) => {
                        if (res.errcode == 0) {
                            this.isLock = false
                            this.visible = false;
                            this.$emit("refreshList");
                            this.$message.success("新增成功");
                        } else {
                            this.isLock = false
                            this.$message.error(res.msg);
                            console.log("接口报错");
                        }
                    })
                    .catch((error) => {
                        console.log("拌合站设置-新增）", error);
                    });
            }
        },
        // 审核
        yj_examine(data, status) {
            this.isLock = true
            if (status == 0) {
                // 计划审核
                CheckCreditEvaluationProcessByOwner(data).then((res) => {
                    if (res.errcode == 0) {
                        this.isLock = false
                        this.visible = false;
                        this.$emit("refreshList");
                    } else {
                        this.isLock = false
                    }
                });
            } else if (status == 1) {
                checkCreditEvaluationProcessByGroup(data).then((res) => {
                    if (res.errcode == 0) {
                        this.isLock = false
                        this.visible = false;
                        this.$emit("refreshList");
                    } else {
                        this.isLock = false
                    }
                });
            }
        },

    },
};